//
//  CZRefreshView.swift
//  刷新控件
//
//  Created by Albert Zhu on 16/10/23.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import UIKit

// 刷新视图，负责和刷新相关的UI和动画
class CZRefreshView: UIView {
    // 转轮
    @IBOutlet weak var tipIcon: UIImageView!
    // 标签
    @IBOutlet weak var tipLabel: UILabel!
    // 转轮
    @IBOutlet weak var indicator: UIActivityIndicatorView!
    
    var refreshState: CZRefreshState = .Normal {
        didSet {
            switch refreshState {
            case .Normal:
                tipLabel.text = "下拉开始刷新..."
                indicator.stopAnimating()
                tipIcon.isHidden = false
                UIView.animate(withDuration: 0.25){
                    self.tipIcon.transform = CGAffineTransform.identity
                }
            case .Pulling:
                tipLabel.text = "松开立即刷新..."
                UIView.animate(withDuration: 0.25){
                    self.tipIcon.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI + 0.001))
                }
            case .WillRefresh:
                tipLabel.text = "正在刷新..."
                tipIcon.transform = CGAffineTransform.identity
                tipIcon.isHidden = true
                indicator.startAnimating()
            }
        }
    }
    
    class func refreshView() -> CZRefreshView {
        
        let nib = UINib(nibName: "CZRefreshView", bundle: nil)
        
        return nib.instantiate(withOwner: nil, options: nil)[0] as! CZRefreshView
    }
}
